-
-
Notifications
You must be signed in to change notification settings - Fork 10
Add RSpecRails/HttpStatusNameConsistency cop
#74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add RSpecRails/HttpStatusNameConsistency cop
#74
Conversation
b3ad323 to
83c0f50
Compare
|
@pirj Thank you for the comment. I think the cop is needed for autocorrecting. (I fixed the CI error by confirming the configuration and documentation.) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
| class HttpStatusNameConsistency < ::RuboCop::Cop::Base | ||
| extend AutoCorrector | ||
|
|
||
| requires_gem 'rack', '>= 3.1.0' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, today I learned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also learned about it here: rubocop/rubocop-rails#1520 (comment)
It's very convenient!
https://docs.rubocop.org/rubocop/development.html#requiring-a-gem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, that's so nice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your contribution!❤️
| class HttpStatusNameConsistency < ::RuboCop::Cop::Base | ||
| extend AutoCorrector | ||
|
|
||
| requires_gem 'rack', '>= 3.1.0' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, that's so nice.
Background
In Rack 3.1, several HTTP status symbol names were changed.
For example, the HTTP status 422 was renamed from
:unprocessable_entityto:unprocessable_content.To align with these changes in Rack, the
scaffoldandscaffold_controllergenerators in therspec-railsgem have also been updated to use:unprocessable_contentinstead of:unprocessable_entity, as shown below:describe "POST /create" do ... context "with invalid parameters" do ... it "renders a response with 422 status (i.e. to display the 'new' template)" do post posts_url, params: { post: invalid_attributes } - expect(response).to have_http_status(:unprocessable_entity) + expect(response).to have_http_status(:unprocessable_content) end end endDetails
This Pull Request adds a new cop that detects the use of obsolete HTTP status symbols in existing test code and enforces consistency by using the current HTTP status names.
The cop checks for symbols defined in
Rack::Utils::OBSOLETE_SYMBOL_MAPPINGS, which currently include:payload_too_largeand:unprocessable_entity.https://github.com/rack/rack/blob/v3.1.0/lib/rack/utils.rb#L573-L576
Additional Information
The
rubocop-railsgem has also introduced a similar cop namedRails/HttpStatusNameConsistency,which enforces consistency by using the current HTTP status names in
app/controllers/*.Rails/HttpStatusNameConsistencycop rubocop-rails#1520Before submitting the PR make sure the following are checked:
master(if not - rebase it).CHANGELOG.mdif the new code introduces user-observable changes.bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).If you have created a new cop:
config/default.yml.Enabled: pendinginconfig/default.yml.VersionAdded: "<<next>>"indefault/config.yml.If you have modified an existing cop's configuration options:
VersionChanged: "<<next>>"inconfig/default.yml.